package com.example.testdemo.view_state;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu;
import android.view.View;

/**
 * Created by 颜 on 05.04.004.
 * View的生命周期执行顺序
 */

public class ViewState extends View {
    private static final String TAG = "VIEW_STATE";
    private OnStateListener onStateListener;

    public void setOnStateListener(OnStateListener onStateListener) {
        this.onStateListener = onStateListener;
    }

    public interface OnStateListener{
        void onState(String str);
    }

    public ViewState(Context context) {
        super(context);
        show("public ViewState(1)");
    }

    private void show(String string) {
        Log.d(TAG, string);
        if (onStateListener != null){
            onStateListener.onState(string);
        }
    }

    public ViewState(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        show("ViewState(2)");
    }

    public ViewState(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        show("ViewState(3)");
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        show("onMeasure" + widthMeasureSpec + " " + heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        show("onLayout" + changed + " " + left + " " + top + " " + right + " " + bottom);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        show("onSizeChanged" + " " + w + " " + h + " " + oldw + " " + oldh );
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        show("onSizeChanged");
    }

    @Nullable
    @Override
    protected Parcelable onSaveInstanceState() {
        show("onSaveInstanceState");
        return super.onSaveInstanceState();
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        show("onWindowFocusChanged");
    }

    @Override
    protected void onDisplayHint(int hint) {
        super.onDisplayHint(hint);
        show("onDisplayHint");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        show("onFinishInflate");
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        show("onDetachedFromWindow");
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        show("onCreateDrawableState");
        return super.onCreateDrawableState(extraSpace);
    }

    @Override
    protected boolean onSetAlpha(int alpha) {
        show("onSetAlpha");
        return super.onSetAlpha(alpha);
    }

    @Override
    protected void onAnimationStart() {
        show("onAnimationStart");
        super.onAnimationStart();
    }

    @Override
    protected void onAnimationEnd() {
        show("onAnimationEnd");
        super.onAnimationEnd();
    }

    @Override
    protected void onAttachedToWindow() {
        show("onAttachedToWindow");
        super.onAttachedToWindow();
    }

    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        show("onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onCreateContextMenu(ContextMenu menu) {
        show("onCreateContextMenu");
        super.onCreateContextMenu(menu);
    }

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
        show("onFocusChanged");
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        show("onRestoreInstanceState");
        super.onRestoreInstanceState(state);
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        show("onVisibilityChanged");
        super.onVisibilityChanged(changedView, visibility);
    }

    @Override
    protected void onWindowVisibilityChanged(int visibility) {
        show("onWindowVisibilityChanged");
        super.onWindowVisibilityChanged(visibility);
    }

    @Override
    public void onDrawForeground(Canvas canvas) {
        show("onDrawForeground");
        super.onDrawForeground(canvas);
    }
}
